package com.miage.kangourou.client.beans;

import java.io.Serializable;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class Utilisateur implements Serializable {

	private static final long serialVersionUID = 1L;

	private static String bytesToHex(byte[] b) {
		char hexDigit[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
				'A', 'B', 'C', 'D', 'E', 'F' };
		StringBuffer buf = new StringBuffer();
		for (int j = 0; j < b.length; j++) {
			buf.append(hexDigit[(b[j] >> 4) & 0x0f]);
			buf.append(hexDigit[b[j] & 0x0f]);
		}
		return buf.toString();
	}

	private int id_utilisateur;
	private String nom;
	private String prenom;
	private String email;
	private String password;
	private String profil;

	private String cookie;

	public Utilisateur() {

		this.id_utilisateur = -1;
		this.nom = "";
		this.prenom = "";
		this.email = "";
		this.password = "";
		this.profil = "";

	}

	public Utilisateur(int id_utilisateur, String nom, String prenom,
			String email, String password, String profil) {
		super();
		this.id_utilisateur = id_utilisateur;
		this.nom = nom;
		this.prenom = prenom;
		this.email = email;
		this.password = password;
		this.profil = profil;

		generateCookie();
	}

	private void generateCookie() {

		try {
			String password = email + this.password;
			MessageDigest md = MessageDigest.getInstance("MD5");
			md.update(password.getBytes());

			byte[] md5 = md.digest();

			cookie = bytesToHex(md5);

		} catch (NoSuchAlgorithmException e) {
			e.printStackTrace();
		}

	}

	public String getCookie() {
		return cookie;
	}

	public String getEmail() {
		return email;
	}

	public int getId_utilisateur() {
		return id_utilisateur;
	}

	public String getNom() {
		return nom;
	}

	public String getPassword() {
		return password;
	}

	public String getPrenom() {
		return prenom;
	}

	public String getProfil() {
		return profil;
	}

	public boolean isAdmin() {

		return (profil.equalsIgnoreCase("admin"));
	}

	public boolean isEmpty() {

		return (id_utilisateur == -1);
	}

	public void kill() {
		this.id_utilisateur = -1;
		this.nom = "";
		this.prenom = "";
		this.email = "";
		this.password = "";
		this.profil = "";

	}

	public void setEmail(String email) {
		this.email = email;
		generateCookie();
	}

	public void setId_utilisateur(int id_utilisateur) {
		this.id_utilisateur = id_utilisateur;
	}

	public void setNom(String nom) {
		this.nom = nom;
	}

	public void setPassword(String password) {
		this.password = password;
		generateCookie();
	}

	public void setPrenom(String prenom) {
		this.prenom = prenom;
	}

	public void setProfil(String profil) {
		this.profil = profil;
	}
}
